local sk__hualong = fk.CreateSkill {

  name = "sk__hualong",

  tags = {  },

}

local function DoQianyuan(player, index, count, skill_name)
  local room = player.room
  if index == 1 then
    room:damage{
      to = player,
      damage = count,
      skillName = skill_name,
    }
  elseif index == 2 then
    room:loseHp(player, count, skill_name)
  elseif index == 3 then
    room:changeMaxHp(player, -count)
  elseif index == 4 then
    room:askForDiscard(player, count, count, true, skill_name, false)
  elseif index == 5 then
    local skills = table.map(table.filter(player.player_skills, function(s)
      return s:isPlayerSkill(player) and s.visible
    end), function(s)
      return s.name
    end)
    if #skills > 0 then
      room:handleAddLoseSkills(player, "-"..table.random(skills), nil, true, false)
    end
  elseif index == 6 then
    room:addPlayerMark(player, MarkEnum.SkInvalidity.."-turn", 1)
  elseif index == 7 then
    player:setChainState(true)
  elseif index == 8 then
    player:turnOver()
  end
end

sk__hualong:addEffect(fk.EventPhaseStart, {
  name = "sk__hualong",
  anim_type = "special",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__hualong.name) and player.phase == Player.Start and #player.room.alive_players > 1 and
      #player:getTableMark("@[sk__qianyuan]") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1,
      "#sk__hualong-choose", sk__hualong.name, true, true)
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self).tos[1])
    local record = table.map(player:getTableMark("@[sk__qianyuan]"), function (str)
      return tonumber(str[13])
    end)
    room:addPlayerMark(player, sk__hualong.name, #record)
    for _, i in ipairs(record) do
      if to.dead then break end
      if i == 4 and to:isNude() then
        --continue
      else
        local count = 1
        if i < 5 then
          count = player:getMark("sk__qianyuan_record"..i)
        end
        DoQianyuan(to, i, count, sk__hualong.name)
      end
    end
    if player.dead then return end
    room:setPlayerMark(player, "@[sk__qianyuan]", 0)
    for i = 1, 4, 1 do
      room:setPlayerMark(player, "sk__qianyuan_record"..i, 0)
    end
    if player.maxHp <= player:getMark(sk__hualong.name) then
      player.maxHp = player:getMark(sk__hualong.name)
      player.hp = player:getMark(sk__hualong.name)
      room:broadcastProperty(player, "maxHp")
      room:broadcastProperty(player, "hp")
    end
    if player:getHandcardNum() < player:getMark(sk__hualong.name) then
      player:drawCards(player:getMark(sk__hualong.name) - player:getHandcardNum(), sk__hualong.name)
    end
  end,
})
sk__hualong:addEffect(fk.DrawNCards, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark(sk__hualong.name) > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.n = player:getMark(sk__hualong.name)
  end,
})

sk__hualong:addEffect("targetmod", {
  name = "#sk__hualong_targetmod",
  residue_func = function(self, player, skill, scope)
    if player:getMark("sk__hualong") > 0 then
      if skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
        return player:getMark("sk__hualong") - 1
      elseif skill.trueName == "analeptic_skill" and scope == Player.HistoryTurn then
        return player:getMark("sk__hualong") - 1
      end
    end
  end,
})

sk__hualong:addEffect("atkrange", {
  name = "#sk__hualong_attackrange",
  fixed_func = function (self, player)
    if player:getMark("sk__hualong") > 0 then
      return player:getMark("sk__hualong")
    end
  end,
})

sk__hualong:addEffect(fk.AfterCardsMove, {
  name = "#sk__hualong_delay",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:getHandcardNum() < player:getMark("sk__hualong") then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(player:getMark("sk__hualong") - player:getHandcardNum(), "sk__hualong")
  end,
})

return sk__hualong